package com.cindata;

import java.math.BigDecimal;
import java.util.Map;


public class Trans
{

	static final double x_pi = 52.359877559829883D;
	static double M_PI = 3.1415926535897931D;

	public Trans()
	{
	}

	public static void main(String args[]) {
		String bdXy = "121.621631, 38.918954";
		double bdx = Double.parseDouble(bdXy.split(",")[0]);
		double dby = Double.parseDouble(bdXy.split(",")[1]);
		String mercXy = BdToMct(bdx, dby);
		System.out.println((new StringBuilder("asdfas  ")).append(mercXy).toString());
		mercXy = "1.296845136421069E7,4860904.119334335";
		String mctX = mercXy.split(",")[0];
		String mctY = mercXy.split(",")[1];
		String bd = MctToBd(mctX, mctY);
		String[] bdzb = bd.split(",");
		Double tmpX = Double.parseDouble(bdzb[0]);
		Double tmpY = Double.parseDouble(bdzb[1]);
		System.out.println(tmpX);

		System.out.println((new StringBuilder("sadfasd")).append(bd).toString());
		System.out.println(BdToMct(126.7426, 45.7418));
	}

	public static String BdToMct(double bdx, double dby) {
		String ef = BdToGcj02(dby, bdx);
		WGSTOGCJ02 w23 = new WGSTOGCJ02();
		double hxx = Double.valueOf(ef.split(",")[1]).doubleValue();
		double hxy = Double.valueOf(ef.split(",")[0]).doubleValue();
		Map s2 = w23.gcj2wgs(hxx, hxy);
		Double lon2 = (Double) s2.get("lon");
		Double lat2 = (Double) s2.get("lat");
		String StrMercXy = WgsToMct(lon2.doubleValue(), lat2.doubleValue());
		return StrMercXy;
	}

	public static String MctToBd(String mctStrX, String mctStrY)
	{
		double mctX = Double.parseDouble(mctStrX);
		double mctY = Double.parseDouble(mctStrY);
		String WgsXy = MctToWgs(mctX, mctY);
		double wgx = Double.parseDouble(WgsXy.split(",")[0]);
		double wgy = Double.parseDouble(WgsXy.split(",")[1]);
		WGSTOGCJ02 w2g = new WGSTOGCJ02();
		Map ss = w2g.transform(wgx, wgy);
		Double lon = (Double)ss.get("lon");
		Double lat = (Double)ss.get("lat");
		String StrMercXy = Gcj02ToBd(lon.doubleValue(), lat.doubleValue());
		return StrMercXy;
	}

	public static String Gcj02ToBd(double gg_lon, double gg_lat)
	{
		double x = gg_lon;
		double y = gg_lat;
		double z = Math.sqrt(x * x + y * y) + 2.0000000000000002E-005D * Math.sin(y * 52.359877559829883D);
		double theta = Math.atan2(y, x) + 3.0000000000000001E-006D * Math.cos(x * 52.359877559829883D);
		double bd_lon = z * Math.cos(theta) + 0.0064999999999999997D;
		double bd_lat = z * Math.sin(theta) + 0.0060000000000000001D;
		return (new StringBuilder(String.valueOf(bd_lon))).append(",").append(bd_lat).toString();
	}

	public static String BdToGcj02(double bd_lat, double bd_lon)
	{
		double x = bd_lon - 0.0064999999999999997D;
		double y = bd_lat - 0.0060000000000000001D;
		double z = Math.sqrt(x * x + y * y) - 2.0000000000000002E-005D * Math.sin(y * 52.359877559829883D);
		double theta = Math.atan2(y, x) - 3.0000000000000001E-006D * Math.cos(x * 52.359877559829883D);
		double gg_lon = z * Math.cos(theta);
		double gg_lat = z * Math.sin(theta);
		return (new StringBuilder(String.valueOf(gg_lat))).append(",").append(gg_lon).toString();
	}

	public static String WgsToMct(double lon, double lat)
	{
		double xy[] = new double[2];
		double x = (lon * 20037508.342789002D) / 180D;
		double y = Math.log(Math.tan(((90D + lat) * M_PI) / 360D)) / (M_PI / 180D);
		y = (y * 20037508.342789002D) / 180D;
		BigDecimal numX = new BigDecimal(x);
		String strX = numX.setScale(6, 4).toPlainString();
		BigDecimal numY = new BigDecimal(y);
		String strY = numY.setScale(6, 4).toPlainString();
		String StrXy = (new StringBuilder(String.valueOf(strX))).append(",").append(strY).toString();
		return StrXy;
	}

	public static String MctToWgs(double mercatorX, double mercatorY)
	{
		double xy[] = new double[2];
		double x = (mercatorX / 20037508.342789002D) * 180D;
		double y = (mercatorY / 20037508.342789002D) * 180D;
		y = (180D / M_PI) * (2D * Math.atan(Math.exp((y * M_PI) / 180D)) - M_PI / 2D);
		String StrXy = (new StringBuilder(String.valueOf(x))).append(",").append(y).toString();
		return StrXy;
	}

	public static double pi = 3.1415926535897932384626;
	public static double xpi = 3.14159265358979324 * 3000.0 / 180.0;
	public static double a = 6378245.0;
	public static double ee = 0.00669342162296594323;
	public static double[] bd09_To_Gcj02(double lat, double lon) {
		double x = lon - 0.0065, y = lat - 0.006;
		double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * xpi);
		double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * xpi);
		double tempLon = z * Math.cos(theta);
		double tempLat = z * Math.sin(theta);
		double[] gps = {tempLat,tempLon};
		return gps;
	}
}